package com.git.hui.yinshua.biz.user.dao.repository;

import com.git.hui.yinshua.api.model.permission.UserRole;
import com.git.hui.yinshua.biz.rest.models.user.req.UserSearchReq;
import com.git.hui.yinshua.biz.user.dao.domain.UserEntity;
import com.git.hui.yinshua.biz.user.dao.mapper.UserMapper;
import com.git.hui.yinshua.biz.user.service.helper.UserRandomGenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户地址
 *
 * @author YiHui
 * @date 2024/6/7
 */
@Repository
public class UserRepository {

    @Autowired
    private UserMapper userMapper;

    public UserEntity queryOrInitByWxId(String unionId, String wxId) {
        UserEntity user = userMapper.queryUserByWxId(wxId);
        if (user != null) {
            // 更新登录时间
            user.setLoginAt(System.currentTimeMillis());
            userMapper.updateUser(user);
            return user;
        }

        // 用户不存在，则新增一个
        user = new UserEntity();
        user.setUname(unionId);
        user.setWxId(wxId);
        user.setRole(UserRole.NORMAL.getRole());
        user.setSource("wx");
        user.setAvatar(UserRandomGenHelper.genAvatar());
        user.setNickname(UserRandomGenHelper.genNickName());
        save(user);
        return user;
    }

    public UserEntity save(UserEntity user) {
        if (user.getAvatar() == null) {
            user.setAvatar("");
        }
        if (user.getPassword() == null) {
            user.setPassword("");
        }
        if (user.getNickname() == null) {
            user.setNickname("");
        }
        if (user.getRole() == null) {
            user.setRole(UserRole.NORMAL.getRole());
        }
        if (user.getWxId() == null) {
            user.setWxId("");
        }
        if (user.getSource() == null) {
            user.setSource("normal");
        }
        if (user.getPhone() == null) {
            user.setPhone("");
        }
        userMapper.saveUser(user);
        return user;
    }

    public UserEntity update(UserEntity user) {
        userMapper.updateUser(user);
        return user;
    }

    public UserEntity queryById(Integer userId) {
        return userMapper.queryUserById(userId);
    }

    public UserEntity queryByUname(String uname) {
        return userMapper.queryUserByUname(uname);
    }

    public List<UserEntity> listByIds(Collection<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return userMapper.listByUsers(ids);
    }

    /**
     * 分页查询用户列表，时间倒排
     *
     * @param search
     * @return
     */
    public List<UserEntity> list(UserSearchReq search) {
        return userMapper.listUser(search.getUname(), search.getNickname(), search.getPhone(), search.getStart(), search.getSize());
    }

    public int count(UserSearchReq search) {
        return userMapper.userCount(search.getUname(), search.getNickname(), search.getPhone());
    }

    /**
     * 用户信息更新
     *
     * @param userId
     * @param nickName
     * @param phone
     * @param avatar
     * @return
     */
    public UserEntity updateUser(Integer userId, String nickName, String phone, String avatar) {
        UserEntity user = new UserEntity();
        user.setId(userId);
        user.setNickname(nickName);
        user.setAvatar(avatar);
        user.setPhone(phone);
        userMapper.updateUser(user);
        return queryById(userId);
    }
}
